package com.fyp.settings;

import java.io.IOException;

import com.fyp.speachrecognition.R;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class SettingsMain extends Activity implements OnInitListener{

	
	private static final String LOG_TAG = "AudioRecordTest";
	private String Directory_Name ;
	private String extention = ".3gp";
	
		
	TabHost tabHost;
	 boolean mStartPlaying = true;
	 boolean mStartRecording = true;
	
	private Button   playUserPrompt = null;
	private Button   playPassPrompt = null;
	private Button   playLoginSuccess = null;
	private Button   playLoginError = null;
	private Button playErrorForUnmatch = null;
	private Button playSavingMenu = null;
	private Button playSavingError=null;
	private Button playsavingContactNoPrompt=null;
	private Button playSavingMessage=null;
	
	private Button playPhoneBookMenu = null;
	private Button playCallComand = null;
	private Button playDeleteComand=null;
	private Button playcontactNotFound=null;
	
	private Button back;
	private Button recordUserPrompt = null;
	private Button recordPassPrompt = null;
	private Button recordLoginSuccess = null;
	private Button recordLoginError = null;
	private Button recordErrorForUnmatch = null;
	private Button recordSavingMenu = null;
	private Button recordSavingError=null;
	private Button recodSavingContactNoPrompt=null;
	private Button recodSavingMessage=null;
	
	private Button recordPhoneBookMenu = null;
	private Button recordCallComand = null;
	private Button recordDeleteComand=null;
	private Button recordcontactNotFound=null;
	
	private MediaPlayer   mPlayer = null;
	private MediaRecorder mRecorder = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.settings_activity);
		Directory_Name = Environment.getExternalStorageDirectory().getAbsolutePath();
		tabHost=(TabHost)findViewById(android.R.id.tabhost);
        tabHost.setup();
        TabSpec spec1=tabHost.newTabSpec("TAB 1");
        spec1.setContent(R.id.LogIn_Recordings);
        spec1.setIndicator("Login Recordings");
     
     
        TabSpec spec2=tabHost.newTabSpec("TAB 2");
        spec2.setIndicator("Calling Recordings");
        spec2.setContent(R.id.speachDialerRecordings);
     
     
        TabSpec spec3=tabHost.newTabSpec("TAB 3");
        spec3.setContent(R.id.SavingRecordings);
        spec3.setIndicator("Saveing Recordings");
        
        TabSpec spec4=tabHost.newTabSpec("TAB 4");
        spec4.setContent(R.id.Phone_Book_Recordings);
        spec4.setIndicator("Phone Book Recordings");
        tabHost.addTab(spec1);
        tabHost.addTab(spec2);
        tabHost.addTab(spec3);
        tabHost.addTab(spec4);
		back =  (Button) findViewById(R.id.backBtn);       
		this.recordUserPrompt = (Button) findViewById(R.id.recordUserPrompt);
		this.playUserPrompt = (Button) findViewById(R.id.playUserPrompt);
		this.recordPassPrompt = (Button) findViewById(R.id.recordPasswordPrompt);
		this.playPassPrompt = (Button) findViewById(R.id.playPassPrompt);
		this.recordLoginSuccess = (Button) findViewById(R.id.recordLoginSuccess);
		this.playLoginSuccess = (Button) findViewById(R.id.playLoginSuccess);
		this.recordLoginError = (Button) findViewById(R.id.RecordLoginError);
		this.playLoginError = (Button) findViewById(R.id.PlayLoginError);
		playErrorForUnmatch = (Button) findViewById(R.id.playErrorForUnmatc);
		recordErrorForUnmatch = (Button) findViewById(R.id.recordErrorForUnmatch);
		recordSavingMenu = (Button) findViewById(R.id.recordSavingMenu);
		playSavingMenu = (Button) findViewById(R.id.playSavingMenu);
		
		playSavingError = (Button) findViewById(R.id.playSavingError);
		recordSavingError = (Button) findViewById(R.id.RecordSavingError);
		recodSavingContactNoPrompt = (Button) findViewById(R.id.playNoSpeak);
		playsavingContactNoPrompt = (Button) findViewById(R.id.recordNoSpeak);
		playSavingMessage = (Button) findViewById(R.id.playSaveComandMessage);
	
		recodSavingMessage  = (Button) findViewById(R.id.recordSavePropmt);
		
		recordPhoneBookMenu = (Button) findViewById(R.id.recordDeleteMenu);
		 recordCallComand = (Button) findViewById(R.id.RecordCallComand);
		recordDeleteComand= (Button) findViewById(R.id.RecordDeleteCommand);
		playPhoneBookMenu = (Button) findViewById(R.id.playDeleteMenu);
		 playCallComand = (Button) findViewById(R.id.PlayCallComand);
		playDeleteComand= (Button) findViewById(R.id.PlayDelletePrompt);
		playcontactNotFound = (Button) findViewById(R.id.playNameNotFoundError);
		recordcontactNotFound = (Button) findViewById(R.id.RecordcontactNotFound);

		setListeners();
        

		
        
        
	};
	
	



	private void setListeners() {
		// TODO Auto-generated method stub
		back.setOnClickListener(new View.OnClickListener() {
			 
            public void onClick(View arg0) {
                                // Closing registration screen
                // Switching to Login Screen/closing register screen
            	finish();
            }
        });
		playUserPrompt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onPlay(mStartPlaying,Directory_Name+"/"+FileNames.File_UserName_Prompt+extention,playUserPrompt,"Play User Name Prompt");
		            if (mStartPlaying)
		            {
		            	playUserPrompt.setText("Stop playing");
		            	recordUserPrompt.setEnabled(false);
		            } 
		            else 
		            {
		            	playUserPrompt.setText("Play User Name Prompt");
		            	recordUserPrompt.setEnabled(true);
		            } 
		            mStartPlaying = !mStartPlaying;
			}
		});
		recordUserPrompt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onRecord(mStartRecording,Directory_Name+"/"+FileNames.File_UserName_Prompt+extention);
				 
		            if (mStartRecording) 
		            {
		            	recordUserPrompt.setText("Stop recording");
		            	playUserPrompt.setEnabled(false);
		            	
		            } else {
		            	recordUserPrompt.setText("Record User Prompt");
		            	playUserPrompt.setEnabled(true);
		            	
		            }
		            mStartRecording = !mStartRecording;
			}
		});
		this.playPassPrompt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onPlay(mStartPlaying,Directory_Name+"/"+FileNames.File_Password_Prompt+extention,playPassPrompt,"Play Password Prompt");
		            if (mStartPlaying)
		            {
		            	playPassPrompt.setText("Stop playing");
		            	recordPassPrompt.setEnabled(false);
		            } 
		            else 
		            {
		            	playPassPrompt.setText("Play Password Prompt");
		            	recordPassPrompt.setEnabled(true);
		            } 
		            mStartPlaying = !mStartPlaying;
			}
		});
		recordPassPrompt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onRecord(mStartRecording,Directory_Name+"/"+FileNames.File_Password_Prompt+extention);
				 
		            if (mStartRecording) 
		            {
		            	recordPassPrompt.setText("Stop recording");
		            	playPassPrompt.setEnabled(false);
		            
		            } else {
		            	recordPassPrompt.setText("Record Password Prompt");
		            	playPassPrompt.setEnabled(true);
		            
		            }
		            mStartRecording = !mStartRecording;
			}
		});
		playLoginSuccess.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onPlay(mStartPlaying,Directory_Name+"/"+FileNames.File_Login_Success+extention,playLoginSuccess,"Play Main Menu");
		            if (mStartPlaying)
		            {
		            	playLoginSuccess.setText("Stop playing");
		            	recordLoginSuccess.setEnabled(false);
		            } 
		            else 
		            {
		            	playLoginSuccess.setText("Play Main Menu");
		            	recordLoginSuccess.setEnabled(true);
		            } 
		            mStartPlaying = !mStartPlaying;
			}
		});
		recordLoginSuccess.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onRecord(mStartRecording,Directory_Name+"/"+FileNames.File_Login_Success+extention);
				 
		            if (mStartRecording) 
		            {
		            	recordLoginSuccess.setText("Stop recording");
		            	playLoginSuccess.setEnabled(false);
		            } else {
		            	recordLoginSuccess.setText("Record Main Menu");
		            	playLoginSuccess.setEnabled(true);
		            }
		            mStartRecording = !mStartRecording;
			}
		});
		playLoginError.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onPlay(mStartPlaying,Directory_Name+"/"+FileNames.File_Login_Error+extention,playLoginError,"Play Login Error");
		            if (mStartPlaying)
		            {
		            	playLoginError.setText("Stop playing");
		            	recordLoginError.setEnabled(false);
		            } 
		            else 
		            {
		            	playLoginError.setText("Play Login Error");
		            	recordLoginError.setEnabled(true);
		            } 
		            mStartPlaying = !mStartPlaying;
			}
		});
		recordLoginError.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onRecord(mStartRecording,Directory_Name+"/"+FileNames.File_Login_Error+extention);
				 
		            if (mStartRecording) 
		            {
		            	recordLoginError.setText("Stop recording");
		            	playLoginError.setEnabled(false);
		            	
		            } 
		            else 
		            {
		            	recordLoginError.setText("Record Login Error");
		            	playLoginError.setEnabled(true);
		            	
		            }
		            mStartRecording = !mStartRecording;
			}
		});
		playErrorForUnmatch.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onPlay(mStartPlaying,Directory_Name+"/"+FileNames.File_Error_For_No_Keyword_Matches+extention,playErrorForUnmatch,"Play Error For No Keyword Matches");
		            if (mStartPlaying)
		            {
		            	playErrorForUnmatch.setText("Stop playing");
		            	recordErrorForUnmatch.setEnabled(false);
		            } 
		            else 
		            {
		            	playErrorForUnmatch.setText("Play Error For No Keyword Matches");
		            	recordErrorForUnmatch.setEnabled(true);
		            } 
		            mStartPlaying = !mStartPlaying;
			}
		});
		recordErrorForUnmatch.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onRecord(mStartRecording,Directory_Name+"/"+FileNames.File_Error_For_No_Keyword_Matches+extention);
				 
		            if (mStartRecording) 
		            {
		            	recordErrorForUnmatch.setText("Stop recording");
		            	playErrorForUnmatch.setEnabled(false);
		            	
		            } else {
		            	recordErrorForUnmatch.setText("Record Error For No Keyword Matches");
		            	playErrorForUnmatch.setEnabled(true);
		            	
		            }
		            mStartRecording = !mStartRecording;
			}
		});
		playSavingMenu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onPlay(mStartPlaying,Directory_Name+"/"+FileNames.File_Saving_Menu+extention,playSavingMenu,"Play Saving Menu");
		            if (mStartPlaying)
		            {
		            	playSavingMenu.setText("Stop playing");
		            	recordSavingMenu.setEnabled(false);
		            } 
		            else 
		            {
		            	playSavingMenu.setText("Play Saving Menu");
		            	recordSavingMenu.setEnabled(true);
		            } 
		            mStartPlaying = !mStartPlaying;
			}
		});
		recordSavingMenu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onRecord(mStartRecording,Directory_Name+"/"+FileNames.File_Saving_Menu+extention);
				 
		            if (mStartRecording) 
		            {
		            	recordSavingMenu.setText("Stop recording");
		            	playSavingMenu.setEnabled(false);
		            	
		            } else {
		            	recordSavingMenu.setText("Record Saving Menu");
		            	playSavingMenu.setEnabled(true);
		            	
		            }
		            mStartRecording = !mStartRecording;
			}
		});
		playSavingError.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onPlay(mStartPlaying,Directory_Name+"/"+FileNames.File_Saving_Error+extention,playSavingError,"Play Saving Error");
		            if (mStartPlaying)
		            {
		            	playSavingError.setText("Stop playing");
		            	recordSavingError.setEnabled(false);
		            } 
		            else 
		            {
		            	playSavingError.setText("Play Saving Error");
		            	recordSavingError.setEnabled(true);
		            } 
		            mStartPlaying = !mStartPlaying;
			}
		});
		recordSavingError.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onRecord(mStartRecording,Directory_Name+"/"+FileNames.File_Saving_Error+extention);
				 
		            if (mStartRecording) 
		            {
		            	recordSavingError.setText("Stop recording");
		            	playSavingError.setEnabled(false);
		            	
		            } else {
		            	recordSavingError.setText("Record Saving Error");
		            	playSavingError.setEnabled(true);
		            	
		            }
		            mStartRecording = !mStartRecording;
			}
		});
		playsavingContactNoPrompt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onPlay(mStartPlaying,Directory_Name+"/"+FileNames.File_Contact_No_Propmt+extention,playsavingContactNoPrompt,"Play Number Speak");
		            if (mStartPlaying)
		            {
		            	playsavingContactNoPrompt.setText("Stop playing");
		            	recodSavingContactNoPrompt.setEnabled(false);
		            } 
		            else 
		            {
		            	playsavingContactNoPrompt.setText("Play Number Speak");
		            	recodSavingContactNoPrompt.setEnabled(true);
		            } 
		            mStartPlaying = !mStartPlaying;
			}
		});
		recodSavingContactNoPrompt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onRecord(mStartRecording,Directory_Name+"/"+FileNames.File_Contact_No_Propmt+extention);
				 
		            if (mStartRecording) 
		            {
		            	recodSavingContactNoPrompt.setText("Stop recording");
		            	playsavingContactNoPrompt.setEnabled(false);
		            	
		            } else {
		            	recodSavingContactNoPrompt.setText("Record Number Prompt");
		        		playsavingContactNoPrompt.setEnabled(true);
		            	
		            }
		            mStartRecording = !mStartRecording;
			}
		});
		playSavingMessage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onPlay(mStartPlaying,Directory_Name+"/"+FileNames.File_Saving_Prompt+extention,playSavingMessage,"Play Saving Prompt");
		            if (mStartPlaying)
		            {
		            	playSavingMessage.setText("Stop playing");
		            	recodSavingMessage.setEnabled(false);
		            } 
		            else 
		            {
		            	playSavingMessage.setText("Play Saving Prompt");
		            	recodSavingMessage.setEnabled(true);
		            } 
		            mStartPlaying = !mStartPlaying;
			}
		});
		recodSavingMessage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onRecord(mStartRecording,Directory_Name+"/"+FileNames.File_Saving_Prompt+extention);
				 
		            if (mStartRecording) 
		            {
		            	recodSavingMessage.setText("Stop recording");
		            	playSavingMessage.setEnabled(false);
		            	
		            } else {
		            	recodSavingMessage.setText("Record Saving Prompt");
		            	playSavingMessage.setEnabled(true);
		            	
		            }
		            mStartRecording = !mStartRecording;
			}
		});

		playPhoneBookMenu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onPlay(mStartPlaying,Directory_Name+"/"+FileNames.File_PhoneBook_Menu+extention,playPhoneBookMenu,"Play PhoneBook Menu");
		            if (mStartPlaying)
		            {
		            	playPhoneBookMenu.setText("Stop playing");
		            	recordPhoneBookMenu.setEnabled(false);
		            } 
		            else 
		            {
		            	playPhoneBookMenu.setText("Play PhoneBook Menu");
		            	recordPhoneBookMenu.setEnabled(true);
		            } 
		            mStartPlaying = !mStartPlaying;
			}
		});
		recordPhoneBookMenu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onRecord(mStartRecording,Directory_Name+"/"+FileNames.File_PhoneBook_Menu+extention);
				 
		            if (mStartRecording) 
		            {
		            	recordPhoneBookMenu.setText("Stop recording");
		            	playPhoneBookMenu.setEnabled(false);
		            	
		            } else {
		            	recordPhoneBookMenu.setText("Record PhoneBook Menu");
		            	playPhoneBookMenu.setEnabled(true);
		            	
		            }
		            mStartRecording = !mStartRecording;
			}
		});
		
playCallComand.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onPlay(mStartPlaying,Directory_Name+"/"+FileNames.File_Delete_Success+extention,playCallComand,"Play Delete Success");
		            if (mStartPlaying)
		            {
		            	playCallComand.setText("Stop playing");
		            	recordCallComand.setEnabled(false);
		            } 
		            else 
		            {
		            	playCallComand.setText("Play Delete Success");
		            	recordCallComand.setEnabled(true);
		            } 
		            mStartPlaying = !mStartPlaying;
			}
		});
		recordCallComand.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onRecord(mStartRecording,Directory_Name+"/"+FileNames.File_Delete_Success+extention);
				 
		            if (mStartRecording) 
		            {
		            	recordCallComand.setText("Stop recording");
		            	playCallComand.setEnabled(false);
		            	
		            } else {
		            	recordCallComand.setText("Record Delete Success");
		            	playCallComand.setEnabled(true);
		            	
		            }
		            mStartRecording = !mStartRecording;
			}
		});
playDeleteComand.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onPlay(mStartPlaying,Directory_Name+"/"+FileNames.File_Delete_Fail+extention,playDeleteComand,"Play Delete Fail");
		            if (mStartPlaying)
		            {
		            	playDeleteComand.setText("Stop playing");
		            	recordDeleteComand.setEnabled(false);
		            } 
		            else 
		            {
		            	playDeleteComand.setText("Play Delete Fail");
		            	recordDeleteComand.setEnabled(true);
		            } 
		            mStartPlaying = !mStartPlaying;
			}
		});
recordDeleteComand.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			// TODO Auto-generated method stub
				 onRecord(mStartRecording,Directory_Name+"/"+FileNames.File_Delete_Fail+extention);
				 
		            if (mStartRecording) 
		            {
		            	recordDeleteComand.setText("Stop recording");
		            	playDeleteComand.setEnabled(false);
		            	
		            } else {
		            	recordDeleteComand.setText("Record Delete Fail");
		            	playDeleteComand.setEnabled(true);
		            	
		            }
		            mStartRecording = !mStartRecording;
			}
		});
playcontactNotFound.setOnClickListener(new OnClickListener() {
	
	@Override
	public void onClick(View v) {
	// TODO Auto-generated method stub
		 onPlay(mStartPlaying,Directory_Name+"/"+FileNames.File_Contact_NotFound+extention,playcontactNotFound,"Play Contact Not Found");
            if (mStartPlaying)
            {
            	playcontactNotFound.setText("Stop playing");
            	recordcontactNotFound.setEnabled(false);
            } 
            else 
            {
            	playcontactNotFound.setText("Play Contact Not Found");
            	recordcontactNotFound.setEnabled(true);
            } 
            mStartPlaying = !mStartPlaying;
	}
});
recordcontactNotFound.setOnClickListener(new OnClickListener() {
	
	@Override
	public void onClick(View v) {
	// TODO Auto-generated method stub
		 onRecord(mStartRecording,Directory_Name+"/"+FileNames.File_Contact_NotFound+extention);
		 
            if (mStartRecording) 
            {
            	recordcontactNotFound.setText("Stop recording");
            	playcontactNotFound.setEnabled(false);
            	
            } else {
            	recordcontactNotFound.setText("Record Contact Not Found");
            	playcontactNotFound.setEnabled(true);
            	
            }
            mStartRecording = !mStartRecording;
	}
});

	}




	private void onRecord(boolean start,String fileName) {
	    if (start) 
	    {
	        startRecording(fileName);
	    } else 
	    {
	        stopRecording();
	    }
	}
	private void onPlay(boolean start,String fileName,Button b,String s) {
	    if (start) 
	    {
	        startPlaying(fileName,b,s);
	    } else 
	    {
	        stopPlaying();
	    }
	}
	private void startPlaying(String fileName,final Button b,final String s) 
	{
	    mPlayer = new MediaPlayer();
	    try {
	        mPlayer.setDataSource(fileName);
	        mPlayer.prepare(); 
	        mPlayer.start();
	        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
				
				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub
					mPlayer.stop();
		    		mPlayer.release();
		    		b.setEnabled(true);
		    		b.setText(s);
		    		mStartPlaying = !mStartPlaying;
		    		recordUserPrompt.setEnabled(true);
		    		recordPassPrompt.setEnabled(true);
		    		recordLoginSuccess.setEnabled(true);
		    		recordLoginError.setEnabled(true);
		    		recordErrorForUnmatch.setEnabled(true);
		    		recordSavingMenu.setEnabled(true);
		    		recordSavingError.setEnabled(true);
		    		recodSavingContactNoPrompt.setEnabled(true);
		    		recodSavingMessage.setEnabled(true);
		    		recordcontactNotFound.setEnabled(true);
		    		playDeleteComand.setEnabled(true);
		    		playCallComand.setEnabled(true);
		    		playPhoneBookMenu.setEnabled(true);
				}
			});
	    } catch (IOException e) {
	        Log.e(LOG_TAG, "prepare() failed");
	    }
	    finally
	    {
	    	
	    		
	    		
	    	
	    }
	}
	private void startRecording(String fileName) {
	    mRecorder = new MediaRecorder();
	    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
	    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
	    mRecorder.setOutputFile(fileName);
	    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

	    try {
	        mRecorder.prepare();
	    } catch (IOException e) {
	        Log.e(LOG_TAG, "prepare() failed");
	    }

	    mRecorder.start();
	}

	private void stopRecording() {
	    mRecorder.stop();
	    mRecorder.release();
	    mRecorder = null;
	}

	private void stopPlaying() {
		mPlayer.stop();
	    mPlayer.release();
	    mPlayer = null;
	}
	
	//============================================
	@Override
	public void onInit(int arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onPause() {
	    super.onPause();
	    if (mRecorder != null) {
	        mRecorder.release();
	        mRecorder = null;
	    }

	    if (mPlayer != null) {
	        mPlayer.release();
	        mPlayer = null;
	    }
	}

}

















